<template>
  <a-dropdown v-if="currentUser && currentUser.name" placement="bottomCenter">
    <div class="ant-pro-account-avatar">
      <img style="width: 30px" src="../../../assets/user.png" />
      <span
        :style="
          navLayout === 'left' ? '' : theme === 'dark' ? 'color: #fff;' : ''
        "
        >&ensp;&ensp;&ensp;{{ currentUser.name }}</span
      >
    </div>
    <template v-slot:overlay>
      <a-menu class="ant-pro-drop-down menu" :selected-keys="[]">
        <!-- <a-menu-item v-if="menu" key="center" @click="handleToCenter">
          <a-icon type="user" />个人信息
        </a-menu-item> -->
        <!-- <a-menu-item v-if="menu" key="settings" @click="handleToSettings">
          <a-icon type="setting" />修改密码
        </a-menu-item>
        <a-menu-divider v-if="menu" /> -->
        <a-menu-item key="logout" @click="handleLogout">
          <a-icon type="logout" />退出登录
        </a-menu-item>
      </a-menu>
    </template>
  </a-dropdown>
  <span v-else>
    <a-spin size="default" :style="{ marginLeft: 8, marginRight: 8 }" />
  </span>
</template>

<script>
import { Modal } from "ant-design-vue";

export default {
  name: "AvatarDropdown",
  props: {
    currentUser: {
      type: Object,
      default: () => null,
    },
    menu: {
      type: Boolean,
      default: true,
    },
    navLayout: {
      type: String,
      default: "left",
    },
    theme: {
      type: String,
      required: true,
    },
  },
  data() {
    return {};
  },
  methods: {
    handleToCenter() {
      // this.$router.push({ path: "/userCenter/userInfo" });
    },
    handleToSettings() {
      // this.$router.push({ path: "/userCenter/changePass" });
    },
    handleLogout() {
      Modal.confirm({
        title: "提示信息",
        content: "确定要登出账户？",
        onOk: () => {
          // return new Promise((resolve, reject) => {
          //   setTimeout(Math.random() > 0.5 ? resolve : reject, 1500)
          // }).catch(() => console.log('Oops errors!'))
          return this.$store.dispatch("Logout").then(() => {
            this.$router.push({ name: "login" });
            window.location.reload();
          });
        },
        onCancel() {},
      });
    },
  },
};
</script>

<style lang="less" scoped>
.ant-pro-account-avatar {
  padding-right: 10px;

  .antd-pro-global-header-index-avatar {
    // margin: ~'calc((@{layout-header-height} - 24px) / 2)' 0;
    margin: 18px 10px 4px 18px;
    color: @primary-color;
    vertical-align: top;
    background: rgba(255, 255, 255, 0.85);
  }
  // span {
  //   vertical-align: middle;
  // }
}
.ant-pro-drop-down {
  /deep/ .action {
    margin-top: 20px;
    margin-right: 8px;
  }
  /deep/ .ant-dropdown-menu-item {
    min-width: 160px;
  }
}
</style>
